
import sys
import indexing.featureindex


def printUsage(args, usage, minArgs=2):
  if len(args) < minArgs or args[1].startswith("-h") or args[1].startswith("--h"):
    print usage
    sys.exit(1)

usageString = """
Usage: python %s COVERAGE_SUMMARY_FILE [CHR[:MIN-MAX]]
"""

if __name__=="__main__":
    printUsage(sys.argv, usageString % sys.argv[0], 2)
    # index = indexing.featureindex.FeatureIndex(sys.argv[1])
    coverageFile = sys.argv[1]
    (qchr, qmin, qmax) = (None, None, None)
    if len(sys.argv)>2:
      query = sys.argv[2]
      if query.find(":")>=0:
        qchr = query.split(":")[0]
        (qmin, qmax) = tuple(query.split(":")[1].split("-"))
        qmin = long(qmin)
        qmax = long(qmax)
      else:
        qchr = query
    query = (qchr, qmin, qmax)
    sample = coverageFile.split("/")[-1].split(".")[0]
    print >> sys.stderr, "# Coverage file =", coverageFile
    print >> sys.stderr, "# Sample =", sample
    print >> sys.stderr, "# Query =", query
    for feature in indexing.featureindex.getFeatures(coverageFile, query):
        details = feature.getDetails()
        if not details.snp: continue
        print "%s\t%ld\t%s\t%s" % (feature.chr, feature.spos, details.hqSnp, feature.content.strip())

